package com.shuoyue.ycgk.views;

/**
 * Created by Administrator on 2016/9/6.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.shuoyue.ycgk.R;

import java.util.ArrayList;
import java.util.List;


public class PaintView2 extends View {
    private List<Path> mPathList = new ArrayList<>(); // 保存涂鸦轨迹的集合

    private Paint mPaint;
    private Path currentPath;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private int bitMapWidth, bitmapHeight;
    private float currentX, currentY;

    float sx = 0f, sy = 0f, ex = 0f, ey = 0f;

    public PaintView2(Context context) {
        super(context);
    }

    public PaintView2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PaintView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    boolean isInited = false;

    Bitmap bitmap;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.bitMapWidth = getMeasuredWidth();
        this.bitmapHeight = getMeasuredHeight();
        if (bitmapHeight == 0 || bitMapWidth == 0) {
            return;
        }
        Log.e("screenWidth", bitMapWidth + "");
        Log.e("screenHeight", bitmapHeight + "");
        if (!isInited && getVisibility() == View.VISIBLE) {
            init();
            isInited = true;
        }
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mBitmap = Bitmap.createBitmap(bitMapWidth, bitmapHeight,
                Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.parseColor("#ff3343"));
        for (Path path : mPathList) { // 绘制涂鸦轨迹
            canvas.drawPath(path, mPaint);
        }

    }

    boolean isFirstTouch = true;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if (isFirstTouch) {
            sx = x;
            ex = x;
            sy = y;
            ey = y;
            isFirstTouch = false;

        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                currentPath = new Path();
                mPathList.add(currentPath);
                currentX = x;
                currentY = y;
                currentPath.moveTo(currentX, currentY);
                resetFW(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                currentX = x;
                currentY = y;
                if (x > 0 && x < bitMapWidth && y > 0 && y < bitmapHeight) {
                    currentPath.quadTo(currentX, currentY, x, y); // 画线
                    // mCanvas.drawColor(color);
                    resetFW(x, y);
                }
                break;
            case MotionEvent.ACTION_UP:
                mCanvas.drawPath(currentPath, mPaint);
                break;
        }

        invalidate();
        return true;
    }

    private void resetFW(float x, float y) {
        if (x > ex) {
            ex = x;
        } else if (x < sx) {
            sx = x;
        }
        if (y > ey) {
            ey = y;
        } else if (y < sy) {
            sy = y;
        }

    }


    // 清除画板
    public void clear() {
        isFirstTouch = true;
        if (mCanvas != null) {
            currentPath.reset();
            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            invalidate();
            if (bitmap != null) {
                bitmap.recycle();
            }
        }
    }

}
